#include "ring_queue.hpp"
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include "Task.hpp"
using namespace ns_ring_queue;
using namespace ns_task;

void* consume(void* args)
{
    RingQueue<Task>* rq = (RingQueue<Task>*)args;
    while (true)
    {
        sleep(1);
        Task t;
        rq->pop(&t);
        t();
        sleep(1);
    }
    
}
void* product(void* args)
{
    RingQueue<Task>* rq = (RingQueue<Task>*)args;
    const std::string ops = "+-*/%";
    while (true)
    {
        int x = rand()%20+1;
        int y = rand()%20+1;
        int opn = rand()%5+1;
        Task t(x,y,ops[opn]);
        std::cout<<"生产的数据是："<<t.Show()<<std::endl;
        rq->push(t);
    }
}

int main()
{
    srand((long long)time(nullptr));
    RingQueue<int>* rq = new RingQueue<int>();
    pthread_t c0,c1,c2,c3,p;
    pthread_create(&c0,nullptr,consume,(void*)rq);
    pthread_create(&c1,nullptr,consume,(void*)rq);
    pthread_create(&c2,nullptr,consume,(void*)rq);
    pthread_create(&c3,nullptr,consume,(void*)rq);
    pthread_create(&p,nullptr,product,(void*)rq);

    pthread_join(c0,nullptr);
    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(c3,nullptr);
    pthread_join(p,nullptr);
}